import pandas as pd
import simplejson
import requests
import numpy as np
import requests
from bs4 import BeautifulSoup
import re
import requests
import simplejson
import pandas as pd
# 热门板块
import requests
import pandas as pd
import json
import requests
import pandas as pd
import json
import simplejson
import time

pd.set_option('display.max_rows', None)
pd.set_option('display.width', 5000)


class url:
    main = "http://api.quchaogu.com//app/subject/billboard?isOpen=1"  # 主网页
    """GET /app/subject/billboard?device_id=a865166028641465&isOpen=0&sortType=0&filter_date=0&from=dxw&apiversion=8.9&dxww=2021072221_8f00cce658&type=0&day=20210721&backgroundcolor=white&vaid=&oaid=&device_id=a865166028641465 HTTP/1.1
    User-Agent: DxwApp:4.6.0.172 | android:5.1.1 | samsung:SM-G9730 | sc:360,640 | did:a865166028641465 | oaid: | vaid: | did:a865166028641465 | idfa: | qd:anzhi | av:8.9 | uid:8%3A8%3A131488136%3A277ab483b7c13bb82196e4104130ef60%3A1%3A1622384568%3A93188552f70e13fa0e17387b8a774651%3A34d89306144206f
    Host: api.quchaogu.com
    Connection: Keep-Alive
    Accept-Encoding: gzip
    Cookie: web_uinfo=%7B%22uname%22%3A%2213804952971%22%2C%22utype%22%3A1%2C%22uactime%22%3A1622384828%7D; app_ua=DxwApp%3A4.6.0.172%20%7C%20android%3A5.1.1%20%7C%20samsung%3ASM-G9730%20%7C%20sc%3A360%2C640%20%7C%20did%3Aa865166028641465%20%7C%20oaid%3A%20%7C%20vaid%3A%20%7C%20did%3Aa865166028641465%20%7C%20idfa%3A%20%7C%20qd%3Aanzhi%20%7C%20av%3A8.9%20%7C%20uid%3A8%253A8%253A131488136%253A277ab483b7c13bb82196e4104130ef60%253A1%253A1622384568%253A93188552f70e13fa0e17387b8a774651%253A34d89306144206f; vistor_uuid=32717565199866433743603; web_qtstr=8%3A8%3A131488136%3A277ab483b7c13bb82196e4104130ef60%3A1%3A1622384568%3A93188552f70e13fa0e17387b8a774651%3A34d89306144206f
    """
    bk_event_id = "http://api.quchaogu.com/app/event/check?code=GN301584"
    bk_event_idcode = "http://api.quchaogu.com/app/event/check?code="

    event_idToYidong = "http://api.quchaogu.com/event/hangye/list?event_id=255&page=1"
    event_idToYidongCode = "http://api.quchaogu.com/event/hangye/list?event_id={}&page=1"
    event_idToYidongCode2 = "http://api.quchaogu.com/event/hangye/list?event_id={}&page=2"
    # 和时间有关
    main_listToday_30 = "http://api.quchaogu.com/bk/rank/index?lhsj=0&pagecount=20&tab=0&orderKey=je30&orderValue=desc&day=&szjh=0&position=5&page=1&type=0&kjjx=0&zg=0"
    main_listTime_day5 = "http://api.quchaogu.com/bk/rank/index?lhsj=0&pagecount=20&tab=0&orderKey=day5&orderValue=desc&day=2021072{}&szjh=0&position=8&page=1&type=0&kjjx=0&zg=0"
    kline_zhulizijin = "http://api.quchaogu.com/hangqing/bankuai/kline?futu_zhibiaos=%E4%B8%BB%E5%8A%9B%E8%B5%84%E9%87%91&check_tab=&sub_id=&is_use_config=1&code=GN301584&time_type=D&zhutu_zhibiao=&get_pankou=1&event_id=&get_zhutu=1&stock_name=&stock_code=GN301584"
    kline_zhulizijincode = "http://api.quchaogu.com/hangqing/bankuai/kline?futu_zhibiaos=%E4%B8%BB%E5%8A%9B%E8%B5%84%E9%87%91&check_tab=&sub_id=&is_use_config=1&code={}&time_type=D&zhutu_zhibiao=&get_pankou=1&event_id=&get_zhutu=1&stock_name=&stock_code={}"

    kline_zhudong = "http://api.quchaogu.com/hangqing/bankuai/kline?futu_zhibiaos=%E4%B8%BB%E5%8A%A8%E5%87%80%E4%B9%B0&check_tab=&sub_id=&is_use_config=1&code=GN301584&time_type=D&zhutu_zhibiao=&get_pankou=1&event_id=&get_zhutu=1&stock_name=&stock_code=GN301584"
    kline_zhudongcode = "http://api.quchaogu.com/hangqing/bankuai/kline?futu_zhibiaos=%E4%B8%BB%E5%8A%A8%E5%87%80%E4%B9%B0&check_tab=&sub_id=&is_use_config=1&code={}&time_type=D&zhutu_zhibiao=&get_pankou=1&event_id=&get_zhutu=1&stock_name=&stock_code={}"
    """orderKey 与positon相互对应
   0
:
{text: "板块", page_id: 1020}
1
:
{text: "涨幅", sort: 1, sort_key: "percent"}
2
:
{text: "主动净买", sort: 1, sort_key: "je"}
3
:
{text: "主力资金", sort: 1, sort_key: "zlje"}
4
:
{text: "涨速", sort: 1, sort_key: "price_speed"}
5
:
{text: "30分主动", sort: 1, sort_key: "je30", sort_val: 1}
6
:
{text: "主动占比", sort: 1, sort_key: "zj_rate"}
7
:
{text: "主力占比", sort: 1, sort_key: "zl_rate"}
8
:
{text: "5日主动", sort: 1, sort_key: "day5"}
9
:
{text: "5日涨幅", sort: 1, sort_key: "p_day5"}
10
:
{text: "10日主动", sort: 1, sort_key: "day10"}
11
:
{text: "10日涨幅", sort: 1, sort_key: "p_day10"}
12
:
{text: "20日主动", sort: 1, sort_key: "day20"}
13
:
{text: "20日涨幅", sort: 1, sort_key: "p_day20"}"""
    """szjh
    current
:
0
key
:
"szjh"
t
:
"双资金"
t_attr
:
"32"
t_attr_color
:
"#f2233b"""
    # bankuai_="/event/hangye/stockfordxw?start_time=930&pagecount=20&last_time=1500&end_time=1500&filter_date=20210722&page=1&code=GN302786"
    bankuai = "http://api.quchaogu.com/event/hangye/stockfordxw?pagecount=20&filter_date=20210722&page=1&code=GN302786"


class get_url:
    def main(url):
        r = requests.get(url)
        if r.status_code == 200:
            r = r.text.encode('utf-8').decode('unicode_escape')
            if simplejson.loads(r, strict=False)["code"] == 10000:  # 成功获取10000
                # print(simplejson.loads(r, strict=False)["code"])
                return simplejson.loads(r, strict=False)
            else:
                print("不是10000,爬取成功，返回代码失败")
        else:
            print("不是200,爬取失败")


class dwg_pan():
    url_time = 'http://api.quchaogu.com/lhbapp/zhangting/index?day=&quanzi_record=&apiversion=8.7&orderKey=bk_name&orderValue=desc'
    url_time_6 = 'http://api.quchaogu.com/lhbapp/zhangting/index?day=&quanzi_record=&apiversion=8.7&&type=6'
    #url_time = 'http://api.quchaogu.com/lhbapp/zhangting/index?day=20210819&quanzi_record=&apiversion=8.7&orderKey=bk_name&orderValue=desc'
    json=get_url.main(url_time)["data"]["stock_list"]["list"]
    def json_time(self,json=json):
        df_list=[]
        df=pd.DataFrame()
        #print("板块合计",len(json))
        for i in range(len(json)):
            df_=pd.DataFrame(json[i])
            print(df_)
            df_text=df_.text[:16].values.tolist()
            print(df_text)
            df_text=df_text+df_[:1]["remark"].values.tolist()
            print(df_text)
        #     df_1hang=df_[:1][["remark","param"]]#[["remark","param","icons","icon_target"]]
        #     print(df_[:1][["remark","param"]].values.tolist()[0][1]["code"])
        #     print(df_[:1][["remark","param"]].values.tolist()[0][0])

            df_bug=df_[:1]["param"].values.tolist()
            print(df_bug)
            df_text.append(df_bug)
            df_4_5=df_[4:5]["param"].values
            print(df_4_5)
            df_text.append(df_4_5)
            #print(df_text)
            #
    #         try:
    #             print(df_[:1]["icons"].values.tolist()[0][0])
    #              df_text.append(df_[:1]["icons"].values.tolist()[0][0])

    #             print(df_[:1]["icon_target"].values.tolist())
    #             df_text.append(df_[:1]["icon_target"].values.tolist())
    #         except:
    #             print("NAN")

            new = pd.DataFrame([df_text])
            #print(new)
            df=pd.concat([df,new])
        df=df.rename(columns = {0: '股票', 1: '现价', 2: '涨停时刻', 3: '几天几板', 4: '板块(涨停数)', 5: '封单(元)', 6: '换手率', 7: '量比', 8: '主力资金', 9: '股性', 10: '开板次数', 11: '板上成交额', 12: '成交额', 13: '流通值', 14: '总市值', 15: '昨涨幅',16: '揭秘',17: 'code',18: '行业'})

        return df
df_p=dwg_pan()
print(df_p.json_time())
